// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-01-09

package info

import (
	"gitee.com/go-libs/perm"
	"gomq/app/authorizer"
	"gomq/app/models"
	"strings"
)

type Response struct {
	Id          int      `json:"id"`
	Name        string   `json:"name"`
	Username    string   `json:"username"`
	Avatar      string   `json:"avatar"`
	Roles       []string `json:"roles"`
	Permissions []string `json:"permissions"`
}

func (o *Response) WithUser(user *models.User) {
	o.Id = user.Id
	o.Username = user.Username
	o.Name = user.Name
	o.Roles = strings.Split(user.Roles, `,`)
	o.Avatar = user.Avatar
}

func (o *Response) WithPermissions(roles []perm.Role) {
	o.Permissions = authorizer.New().PermissionContainer().GetPermissionsList(roles...)
}
